module executor.message.HistoryMsgExecutor;

import msgtrans;
import message.Constants;
import message.MsgDefine;
import google.protobuf;
import hunt.logging;
import std.array;
import std.conv : to;
import hunt.util.Serialize;
import client.GateServerClient;
import client.MsgServerClient;
import service.HistoryService;

/**
历史纪录相关模块
**/

@TransportServer(LogicServerName)
class HistoryMsgExecutor : AbstractExecutor!(HistoryMsgExecutor) {

  //插入一条历史记录接口
  @MessageId(MESSAGE.CMD_PUT_HISTORY_MSG)
  void putHistoryMsg(TransportContext ctx, MessageBuffer buffer) {
    auto req = new Msg();
    logInfo("CMD_PUT_HISTORY_MSG ----- ");
    buffer.data.fromProtobuf!Msg(req);

    HistoryService.putHistoryMsg(req);
  }

  //获取会话列表接口
  @MessageId(MESSAGE.CMD_GET_SESSION_LIST)
  void getSessionList(TransportContext ctx, MessageBuffer buffer) {
    auto req = new SessionList();
    logInfo("getSessionList ----- ");
    buffer.data.fromProtobuf!SessionList(req);
    SessionListRes res = HistoryService.getSessionList(req.clientId);
    res.src = req.src;
    res.tagId = req.tagId;
    res.clientId = req.clientId;
    ctx.session.send(new MessageBuffer(MESSAGE.CMD_GET_SESSION_LIST_RES,res.toProtobuf.array));
  }



  //获取历史记录接口
  @MessageId(MESSAGE.CMD_GET_HISTORY_REQ)
  void getHistoryMsg(TransportContext ctx, MessageBuffer buffer) {
    auto req = new HistoryMsgREQ();
    logInfo("CMD_GET_HISTORY_REQ ----- ");
    buffer.data.fromProtobuf!HistoryMsgREQ(req);
    Msg[] msgs = HistoryService.getHistoryMsg(req);
    HistoryMsgRES res = new HistoryMsgRES;
    res.tagId = req.tagId;
    res.msgs = msgs;
    res.clientId = req.clientId;
    res.src = req.src;
    ctx.session.send(new MessageBuffer(MESSAGE.CMD_GET_HISTORY_RES,res.toProtobuf.array));
  }
}

